<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_account_payment extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation', 'uri'));
		$this->load->model("payments_model", "model", TRUE);
	}

	function index(){
		$data['countries'] = $this->model->get_countries();
		$data['dealers'] = $this->model->get_dealers();
		$data['test'] = '';
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		$this->template->load("layout_register_pay", "create_account_payment_view", $data);
	}


	function terms(){
		$this->template->load("layout", "terms", '');
	}

	function username_check($str)
	{
		if ($str == 'test')
		{
			$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
			return FALSE;
		}
		$r = $this->model->check_user_exist($str);
		switch($r){
			case -1 : {
				$this->form_validation->set_message('username_check', 'User with this login already exists');
				return FALSE;
			}
			case 1:{
				return TRUE;
			}
		}
	}
	function save(){
		// 	  $this->form_validation->set_rules('login', 'Username', 'callback_username_check');
		$this->form_validation->set_rules('fName', 'First Name ', 'required|min_length[2]|max_length[30]');
		$this->form_validation->set_rules('lName', 'Last Name', 'required|min_length[2]|max_length[30]');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|matches[verifyEmail]');
		$this->form_validation->set_rules('verifyEmail', 'Verify Email', 'required');

		$this->form_validation->set_rules('login', 'Username', 'required|min_length[6]|max_length[15]|callback_username_check');
		$this->form_validation->set_rules('passwd', 'Password', 'required|min_length[6]|max_length[15]|matches[passwdVerify]');

		$this->form_validation->set_rules('addressLine1', 'Address', 'trim|required|min_length[5]|max_length[40]');
		$this->form_validation->set_rules('cityMain', 'City/Town', 'trim|required|min_length[3]|max_length[40]');
		$this->form_validation->set_rules('countryMain', 'Country', 'trim|required');
		$this->form_validation->set_rules('homePhoneMain1', 'Home Phone Country Code', 'trim|required|max_length[6]|is_natural');
		$this->form_validation->set_rules('homePhoneMain2', 'Home Phone Region Code', 'trim|required|max_length[6]|is_natural');
		$this->form_validation->set_rules('homePhoneMain3', 'Home Phone Number', 'trim|required|min_length[5]|max_length[10]|is_natural');

		/*rules for business form*/
		if($this->input->post("accountTypeSelector") == 'business'){
			$this->form_validation->set_rules('businessName', 'Business Name ', 'required|min_length[5]|max_length[40]');
			$this->form_validation->set_rules('addressLine1Business', 'Business Address ', 'required|min_length[5]|max_length[40]');
			$this->form_validation->set_rules('countryBusiness', 'Business Registration Country', 'trim|required');
			$this->form_validation->set_rules('emailBusiness', ' Business Email', 'trim|required|valid_email');

			$this->form_validation->set_rules('businessPhone1', 'Business Phone Country Code', 'trim|required|max_length[6]|is_natural');
			$this->form_validation->set_rules('businessPhone2', 'Business Phone Region Code', 'trim|required|max_length[6]|is_natural');
			$this->form_validation->set_rules('businessPhone3', 'Business Phone Number', 'trim|required|min_length[5]|max_length[10]|is_natural');

			$this->form_validation->set_rules('regNumber', 'Business Registration Number', 'trim|required|min_length[6]|max_length[25]');
			$this->form_validation->set_rules('businessType', 'Business Type', 'trim|required|min_length[3]|max_length[30]');
			$this->form_validation->set_rules('revenue', 'Anual revenue', 'trim|required|min_length[3]|max_length[20]|is_natural');
			$this->form_validation->set_rules('transactionsNr', 'No Of transactions per week', 'trim|required|max_length[10]|is_natural');
			$this->form_validation->set_rules('avgTransactionSize', 'Average size of each transaction ', 'trim|required|max_length[10]|is_natural');
			$this->form_validation->set_rules('currencyTraded', 'Major Curencies Traded', 'required');

			$this->form_validation->set_rules('businessRegDay', 'Business Registration Day', 'trim|required');
			$this->form_validation->set_rules('businessRegMonth', 'Business Registration Month', 'trim|required');
			$this->form_validation->set_rules('businessRegYear', 'Business Registration Year', 'trim|required');
			$this->form_validation->set_rules('businessOwner', 'Business Owner', 'trim|required|min_length[5]|max_length[35]');
			$this->form_validation->set_rules('contactPerson', 'Business Contact Person', 'trim|required|min_length[5]|max_length[35]');
		}

		$this->form_validation->set_rules('availableCurrency', 'Which currency do you have', 'trim|required');
		$this->form_validation->set_rules('requiredCurrency', 'Which currency do you require', 'trim|required');
		$this->form_validation->set_rules('transferAmount', 'Ammount to be transfered', 'trim|required|max_length[10]|is_natural');

		$this->form_validation->set_rules('motherMName', 'Mother\' maiden name', 'trim|required|min_length[2]|max_length[25]');
		$this->form_validation->set_rules('birthPlace', 'Your birth place', 'trim|required|min_length[3]|max_length[40]');
		$this->form_validation->set_rules('passNumber', 'Your Passport number', 'trim|required|min_length[10]|max_length[40]');

		$this->form_validation->set_rules('birthDay', 'Birth Day', 'trim|required');
		$this->form_validation->set_rules('birthMonth', 'Birth Month', 'trim|required');
		$this->form_validation->set_rules('birthYear', 'Birth Year', 'trim|required');
		$this->form_validation->set_rules('accept', 'Terms of Service', 'trim|required');



		if ($this->form_validation->run() == FALSE){
			$data['error'] = validation_errors();
			$data['link'] = site_url('create_account_payment#message');
		}
		else{
			$user = new User_payment;

			$user->USER_TYPE = "IP";
			$user->ACCOUNT_TYPE = $this->input->post('accountTypeSelector');
			$user->USER_ID = $this->input->post('login');
			$user->USER_PASSWD = sha1($this->input->post('passwd'));
			$user->TITLE = $this->input->post('title');
			$user->FIRST_NAME = $this->input->post('fName');
			$user->LAST_NAME = $this->input->post('lName');
			$user->EMAIL = $this->input->post('email');

			$user->HOME_PHONE = $this->input->post('homePhoneMain1') . "-" . $this->input->post('homePhoneMain2') . "-" . $this->input->post('homePhoneMain3');
			$user->MOBILE_PHONE = $this->input->post('mobilePhoneMain1') . "-" . $this->input->post('mobilePhoneMain2') . "-" . $this->input->post('mobilePhoneMain3');
			$user->FAX = $this->input->post('faxMain1') . "-" . $this->input->post('faxMain2') . "-" . $this->input->post('faxMain3');
			$user->STATE = $this->input->post('regionMain');
			$user->COUNTRY_CODE = $this->input->post('countryMain');
			$user->ADDRESS = $this->input->post('addressLine1') . "  " . $this->input->post('addressLine2');
			$user->CITY = $this->input->post('cityMain');
			$user->STATE = $this->input->post('regionMain');
			$user->POSTAL_CODE = $this->input->post('pCodeMain');

			if($this->input->post('kashfxdealer') == 'No'){
				$user->DEALER_ID = 0;
			}
			else{
				$user->DEALER_ID = $this->input->post('kashfxdealer');
			}

			$user->AVAILABLE_CURRENCY = $this->input->post('availableCurrency');
			$user->REQUIRED_CURRENCY = $this->input->post('requiredCurrency');
			$user->INITIAL_DEPOSIT = $this->input->post('transferAmount');
			$user->BIRTHDATE = date("Y-m-d", strtotime($this->input->post('birthDay') . "/" . $this->input->post('birthMonth') . "/" . $this->input->post('birthYear')));
			$user->PROPERTY_TRANS = $this->input->post('isPropertyTrans');
			$user->BUSINESS_TRANS = $this->input->post('isBusinessTrans');
			$user->OVERSEAS = $this->input->post('isRegularPayment');
			$user->PREPAID_CARD = $this->input->post('isPrepaidCard');
			$user->REFERER = $this->input->post('referer');
			$user->MM_NAME = $this->input->post('motherMName');
			$user->BIRTH_PLACE = $this->input->post('birthPlace');
			$user->PASS_NUMBER = $this->input->post('passNumber');
			$user->COMMENTS = $this->input->post('comments');



			if($this->input->post('accountTypeSelector') !=  'business'){
				$user->BUSINESS_ID = 0;
			}
			else{
				/*prepping the data for business*/
				$bizz = new Business();

				$bizz->B_NAME = $this->input->post('businessName');
				$bizz->ADDRESS = $this->input->post('addressLine1Business') . "  " . $this->input->post('addressLine2Business');
				$bizz->COUNTRY_ID = $this->input->post('countryBusiness');
				$bizz->EMAIL = $this->input->post('emailBusiness');
				$bizz->PHONE = $this->input->post('businessPhone1') . "-" . $this->input->post('businessPhone2') . "-" .$this->input->post('businessPhone3');
				$bizz->REGISTRATION_NR = $this->input->post('regNumber');
				$bizz->BUSINESS_TYPE = $this->input->post('businessType');
				$bizz->ANUAL_REVENUE = $this->input->post('revenue');
				$bizz->WEEK_TRANS = $this->input->post('transactionsNr');
				$bizz->TRANS_SIZE = $this->input->post('avgTransactionSize');
				$currencies = $this->input->post('currencyTraded');

				if($currencies){
					foreach($currencies as $item){
		    $bizz->MCURRENCIES_TRADED .= $item . ";";
					}
				}
				else{
					$bizz->MCURRENCIES_TRADED = "";
				}

				$bizz->REG_DATE = date("Y-m-d", strtotime($this->input->post('businessRegDay') . "/" . $this->input->post('businessRegMonth') . "/" . $this->input->post('businessRegYear')));
				$bizz->OWNER = $this->input->post('businessOwner');
				$bizz->BUSINESS_NATURE = $this->input->post('businessNature');
				$bizz->CONTACT_PERSON = $this->input->post('contactPerson');

				$user->BUSINESS_ID = $this->model->save_business($bizz);
			}
			 
			$data['link'] = site_url('create_account_payment#message');
			$data['return'] = "user ID:" . $this->model->save($user);
			$data['success'] = "Your <b>International Payment Account </b> was created successfully!";
			 
		}
		echo json_encode($data);
	}
}

class Business {
	public $B_NAME = "";
	public $COUNTRY_ID = "";
	public $ADDRESS = "";
	public $EMAIL = "";
	public $PHONE = "";
	public $REGISTRATION_NR = "";
	public $BUSINESS_TYPE = "";
	public $ANUAL_REVENUE = 0;
	public $WEEK_TRANS = 0;
	public $TRANS_SIZE = "";
	public $MCURRENCIES_TRADED = "";
	public $REG_DATE = "";
	public $OWNER = "";
	public $BUSINESS_NATURE = "";
	public $CONTACT_PERSON = "";
}
class User_payment{
	public $ACCOUNT_TYPE = "";
	public $USER_ID = "";
	public $USER_TYPE = "";
	public $USER_PASSWD = "";
	public $TITLE = "";
	public $FIRST_NAME = "";
	public $LAST_NAME = "";
	public $EMAIL = "";
	public $HOME_PHONE = "";
	public $MOBILE_PHONE = "";
	public $FAX = "";
	public $COUNTRY_CODE = "";
	public $ADDRESS = "";
	public $CITY = "";
	public $STATE = "";
	public $POSTAL_CODE = "";
	public $BUSINESS_ID = 0;
	public $DEALER_ID = 0;
	public $AVAILABLE_CURRENCY = 0;
	public $REQUIRED_CURRENCY = 0;
	public $BIRTHDATE = "";
	public $INITIAL_DEPOSIT = "";
	public $PROPERTY_TRANS = 0;
	public $BUSINESS_TRANS = 0;
	public $OVERSEAS = 0;
	public $PREPAID_CARD = 0;
	public $REFERER = "";
	public $MM_NAME = "";
	public $BIRTH_PLACE = "";
	public $PASS_NUMBER = "";
	public $COMMENTS = "";

}



